package com.sothink.gUI
{
	import com.sothink.gUI.base.IComposite;
	import com.sothink.gUI.base.IContainer;
	import com.sothink.gUI.base.IControl;
	import com.sothink.gUI.containers.Container;
	import com.sothink.gUI.render.R_Composite;
	import com.sothink.gUI.render.R_Container;
	import com.sothink.gUI.render.R_Control;
	
	import flash.display.Sprite;
	import flash.utils.Dictionary;

	/**
	 * 最上层窗口管理器，包括弹出菜单及浮动窗口，协同UIManager工作
	 * @author Macro776@gmail.com
	 * 
	 */
	public class PopupManager extends Container
	{
		private var _popupControlsMap:Dictionary;
		
		private var _popupContainer:IContainer;
		
		private var _popupSprite:Sprite;
		
		
		private var _modal:Boolean;
		
		
		public function PopupManager()
		{
			_popupControlsMap = new Dictionary(true);
		}

		public function get modal():Boolean
		{
			return _modal;
		}

		
		public function init():void
		{
			
		}
		
		/**
		 * 渲染控件
		 * @param container
		 *
		 */
		private function render(container:IComposite):void
		{
			var parent:R_Composite = _popupControlsMap[container];
			for each (var child:IControl in container.children)
			{
				if (child is IContainer)
				{
					var subcontainer:R_Container = _popupControlsMap[child];
					if (!subcontainer)
					{
						subcontainer = new R_Container(IContainer(child));
						render(IContainer(child));
						_popupControlsMap[child] = subcontainer;
					}
					
					parent.container.addChild(subcontainer.canvas);
					parent.container.addChild(subcontainer.container);
					parent.container.addChild(subcontainer.mask);
					parent.container.addChild(subcontainer.cover);
					
				}
				else if (child is IComposite)
				{
					var subcomposite:R_Composite = _popupControlsMap[child];
					if (!subcomposite)
					{
						subcomposite = new R_Composite(IComposite(child));
						render(IComposite(child));
						_popupControlsMap[child] = subcomposite;
					}
					
					parent.container.addChild(subcomposite.canvas);
					parent.container.addChild(subcomposite.container);
					parent.container.addChild(subcomposite.mask);
					
				}
				else if (child is IControl)
				{
					var subcontrol:R_Control = _popupControlsMap[child];
					if (!subcontrol)
					{
						subcontrol = new R_Control(child);
						_popupControlsMap[child] = subcontrol;
					}
					
					parent.container.addChild(subcontrol.canvas);
					
				}
			}
		}
		
		/**
		 * 销毁弹出容器
		 *
		 */
		public function disposePopupSprite():void
		{
			for each (var child:R_Control in _popupControlsMap)
			child.dispose();
			
			_popupControlsMap = new Dictionary(true);
			_popupContainer = null;
			_popupSprite = null;
		}
		
		
		/**
		 * 添加弹出窗口
		 * @param window
		 * @param modal 是否模态
		 * 
		 */
		public function addPopupWindow(window:IContainer, modal:Boolean):void
		{
			
		}
		
		/**
		 * 添加弹出菜单
		 * @param menu
		 *
		 */
		public function addPopupMenu(menu:IControl):void
		{
			
		}
		
		/**
		 * 移除弹出菜单或窗口
		 * @param popupItem
		 *
		 */
		public function removePopup(popupItem:IControl):void
		{
			
		}
	}
}